//index.js
import api from '../../utils/api.js'
//获取应用实例
var app = getApp();
var order = ['red', 'yellow', 'blue', 'green', 'red']
Page({
  data: {
    imageurl:api.imageurl,
    banner: [],//banner
    classify: [],//菜谱分类
    cookbooks: [],//精品菜品
    newcookbooks: [], //最新菜品
    page: 1,//最新菜品页数
  },
  onLoad: function () {
    var that = this;
    api.getIndexData({
      success: function (res) {
        // console.log(res)
        var data = res.data.result;
        // console.log("data",data)
        var banner = data.banner;
        var classify = data.classify;
        var cookbooks = data.jingcookbooks;
        that.setData({
          banner: banner,
          classify: classify,
          cookbooks: cookbooks,
        })
      }
    })
    that.getNewcookbooks()

  },
  /**
   * 获取最新菜品数据
   */
  // getNewcookbooks:function(){
  //   var that = this;
  //   api.getNewcookbooks({
  //     data: {
  //       page: that.data.page,
  //     },
  //     success: function (res) {
  //       // console.log(res)
  //       var newcookbooks = res.data.result;
  //       // console.log("data", newcookbooks)
  //       var newcookbookss = that.data.newcookbooks
  //       for (var i in newcookbooks) {
  //         newcookbookss.push(newcookbooks[i])
  //       }
  //       that.setData({
  //         newcookbooks: newcookbookss,
  //         page: that.data.page + 1
  //       })
  //     }
  //   })

  // },
  //点击banner跳转链接，若无id,则不跳转
  clickBanner: function (e) {
    var cookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../cookbook/detail/detail?cookId=' + cookId 
    });
  },

// 进入菜谱分类页
  showClassify:function(e){
    var classifyId = e.currentTarget.dataset.classifyId;
    var name = e.currentTarget.dataset.name;

    wx.navigateTo({
      url: "../cookbook/index/index?classifyId=" + classifyId + "&name=" + name 
    });

  },
  // 查看详情
  showDetail: function (e) {
    var cookId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var praiseCount = e.currentTarget.dataset.praise;
    wx.navigateTo({
      url: '../cookbook/detail/detail?cookId=' + cookId + "&name=" + name + "&praise=" + praiseCount
    })
  },
  onReachBottom: function () {
    var that = this;
    that.getNewcookbooks();
  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index',
    }
  },

})
